home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / os2 / monte.zip / PIPES / CLIENT.C next >
Text File  |  1993-03-05  |  2KB  |  72 lines

  1. /* client.c
  2.  
  3. This is the process which is on the client end of the named pipe.
  4. This program is one of two closely-cooperating processes: client.exe
  5. and host.exe.
  6.  
  7. Pipe clients use standard file I/O API's, so this program could be
  8. a DOS program.
  9.  
  10. The client process opens the pipe, gets a string from the console,
  11. then writes the string to the named pipe.  It then reads the pipe
  12. and prints the contents of the read.
  13.  
  14. The client then closes the pipe and exits.
  15.  
  16. */
  17.  
  18.  
  19. #define INCL_BASE
  20. #include <os2.h>
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <malloc.h>
  26. #include <assert.h>
  27.  
  28.  
  29. int main( int argc, char *argv[] )
  30. {
  31.   APIRET  rc;
  32.   HFILE   hPipe;
  33.   ULONG   bytes;
  34.   ULONG   ulAction;
  35.   char    szWork[ 256 ];
  36.  
  37.   // argument is the pipe name to host
  38.   if( argc != 2 ) {
  39.     printf( "supply pipe name on command line like \\PIPE\\FRED\n" );
  40.     return 1;
  41.   }
  42.  
  43.   // use file I/O APIs to open the pipe like a file
  44.   rc = DosOpen( argv[ 1 ], &hPipe, &ulAction, 0, 0, FILE_OPEN,
  45.       OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYREADWRITE | OPEN_FLAGS_FAIL_ON_ERROR, NULL );
  46.   printf( "DosOpen rc %d\n", rc );
  47.   assert( rc == 0 );
  48.  
  49.   // pull a string from the keyboard
  50.   printf( "enter a string: " );
  51.   fflush(NULL);
  52.   gets( szWork );
  53.  
  54.   // write string to pipe as host process reads it
  55.   rc = DosWrite( hPipe, szWork, strlen( szWork ), &bytes );
  56.   assert( rc == 0 );
  57.  
  58.   // read from pipe as host process writes it
  59.   rc = DosRead( hPipe, szWork, sizeof( szWork ), &bytes );
  60.   assert( rc == 0 );
  61.  
  62.   // null-end the string and print it
  63.   szWork[ bytes ] = 0;
  64.   printf( "%s\n", szWork );
  65.  
  66.   // close the pipe like a file
  67.   rc = DosClose( hPipe );
  68.   assert( rc == 0 );
  69.  
  70.   return 0;
  71. }
  72.